import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn, Index } from 'typeorm';
import { TableInfo } from './tableInfo.entity';

@Index('idx_parentId_code',['parentId','code'],{unique:true})
@Entity('field_info') // 请替换为实际的表名
export class FieldInfo {
    @PrimaryGeneratedColumn({ name: 'id', type: 'int', comment: '主键ID' })
    id!: number;


    @Column({
        name: 'status',
        type: 'tinyint',
        default: 1,
        nullable: false,
        comment: '状态(0-禁用,1-正常)'
    })
    status?: number = 1;

    @CreateDateColumn({
        name: 'created_at',
        type: 'datetime',
        default: () => 'CURRENT_TIMESTAMP',
        nullable: false,
        comment: '创建时间',
        precision: 0
    })
    createdAt!: Date;

    @UpdateDateColumn({
        name: 'updated_at',
        type: 'datetime',
        default: () => 'CURRENT_TIMESTAMP',
        onUpdate: 'CURRENT_TIMESTAMP',
        nullable: false,
        comment: '更新时间',
        precision: 0
    })
    updatedAt!: Date;

    @Column({
        name: 'comment',
        type: 'varchar',
        length: 255,
        nullable: true,
        comment: '备注'
    })
    comment?: string | null;

    @Column({
        name: 'code',
        type: 'varchar',
        length: 255,
        comment: '字段代码',
        nullable: false,
    })
    code!: string;

    @Column({
        name: 'name',
        type: 'varchar',
        length: 255,
        comment: '字段名称',
        nullable: false,
    })
    name!: string;

    @Column({
        name: 'data_type',
        type: 'varchar',
        length: 20,
        comment: '数据类型',
        nullable: false
    })
    dataType!: string; // varchar | datetime | int | bigint | tinyint | text | dec | time | date | longtext

    @Column({
        name: 'data_type_length',
        type: 'bigint',
        nullable: true,
        comment: '数据长度'
    })
    dataTypeLength?: number;

    @Column({
        name: 'is_key',
        type: 'tinyint',
        default: 0,
        comment: '是否为主键'
    })
    isKey?: number = 0; // 0 | 1

    @Column({
        name: 'is_null',
        type: 'tinyint',
        default: 0,
        comment: '是否可为空'
    })
    isNull?: number = 0; // 0 | 1

    @Column({
        name: 'is_insert',
        type: 'tinyint',
        default: 0,
        comment: '是否可插入'
    })
    isInsert?: number = 0; // 0 | 1

    @Column({
        name: 'is_edit',
        type: 'tinyint',
        default: 0,
        comment: '是否可编辑'
    })
    isEdit?: number = 0; // 0 | 1

    @Column({
        name: 'is_list',
        type: 'tinyint',
        default: 0,
        comment: '是否在列表显示'
    })
    isList?: number = 0; // 0 | 1

    @Column({
        name: 'is_query',
        type: 'tinyint',
        default: 0,
        comment: '是否可查询'
    })
    isQuery?: number = 0; // 0 | 1

    @Column({
        name: 'query_type',
        type: 'varchar',
        length: 20,
        nullable: true,
        comment: '查询方式'
    })
    queryType?: string | null; // EQ | NE | GT | GTE | LT | LTE | LIKE | BETWEEN

    @Column({
        name: 'is_required',
        type: 'tinyint',
        default: 0,
        comment: '是否必填'
    })
    isRequired?: number = 0; // 0 | 1

    @Column({
        name: 'html_type',
        type: 'varchar',
        length: 50,
        nullable: true,
        comment: '显示类型'
    })
    htmlType?: string | null; // input | textarea | select | radio | checkbox | datetime | imageUpload | fileUpload | editor

    @Column({
        name: 'parent_id',
        type: 'int',
        nullable: false,
        comment: '父级字段ID'
    })
    parentId!: number;

    @Column({
        name: 'sort_order',
        type: 'int',
        default: 0,
        comment: '排序权重，数值越小越靠前，默认0'
    })
    sortOrder?: number;


    @Column({
        name: 'default_value',
        type: 'varchar',
        length: 50,
        comment: '默认值',
        nullable: true,
    })
    defaultValue?: string;

    @ManyToOne(() => TableInfo, tableInfo => tableInfo.fieldInfoList,{
        createForeignKeyConstraints: false // 不创建外键约束

    })
    @JoinColumn({ name: 'parent_id' }) 
    tableInfo!: TableInfo;

    /**小驼峰字段名，不作为表字段 */
    smallHumpCode?:string;
}